package org.gjy.m8.concurrent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-06-30 14:41:58
 */
public class CountDownLatch2Test {
    private static final Logger log = LoggerFactory.getLogger(CountDownLatch2Test.class);

    public static void main(String[] args) {
        log.info("百米跑比赛开始");

        int playerNum = 3;
        CountDownLatch gun = new CountDownLatch(1);
        ExecutorService threadPool = Executors.newFixedThreadPool(playerNum);

        for (int i = 0; i < playerNum; i++) {
            final int playNo = i;

            threadPool.submit(() -> {
                log.info("[{}]号运动员已就绪", playNo);
                try {
                    gun.await();
                } catch (InterruptedException e) {
                    log.info("[{}]号运动员线程阻塞失败，失败原因[{}]", playNo, e.getMessage(), e);
                }
                log.info("[{}]号运动员已经到达重点", playNo);
            });
        }
        log.info("countDown");
        //按下枪 所有运动员起跑
        gun.countDown();

        threadPool.shutdown();
        while (!threadPool.isTerminated()) {
        }
        log.info("百米赛跑已结束");
    }

}
